﻿using System;
using System.Collections.Generic;
using Engine;
using Engine.Graphics;
using static Game.SubsystemCreatureSpawn;
using Game;

namespace IndustrialAge
{
    public class IndustrialDustyLoader : ModLoader
    {
		public static ModEntity ModEntity { get; set; }

        public override void __ModInitialize()
        {
			ModEntity = Entity;
			ModsManager.RegisterHook("OnLoadingStart", this);
			ModsManager.RegisterHook("OnLoadingFinished", this);
			//ModsManager.RegisterHook("InitializeCreatureTypes", this);
		}

        public override void OnLoadingStart(List<Action> actions)
        {
			actions.Add(ItemBlock.InitItems);
        }

        public override void OnLoadingFinished(List<Action> actions)
        {
			actions.Add(delegate {
                ItemBlock.Texture = ContentManager.Get<Texture2D>("IndustrialMod");
            });
			actions.Add(Item.CRInitialize);
			actions.Add(Item.ResetRecipaediaScreens);
		}

		public override void InitializeCreatureTypes(SubsystemCreatureSpawn spawn, List<SubsystemCreatureSpawn.CreatureType> m_creatureTypes)
        {
			m_creatureTypes.Add(new CreatureType("T-100", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
			{
				SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
				{
					float num = spawn.m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z);
					int temperature = spawn.m_subsystemTerrain.Terrain.GetTemperature(point.X, point.Z);
					int humidity = spawn.m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z);
					int num2 = Terrain.ExtractContents(spawn.m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z));
					return (num > 40f && Utils.SubsystemGameInfo.WorldSettings.AreSupernaturalCreaturesEnabled && (num2 == 8 || num2 == 2 || num2 == 3 || num2 == 7 || num2 == 12) && Utils.SubsystemTimeOfDay.Day > 60) ? 0.15f : 0f;
				},
				SpawnFunction = ((CreatureType creatureType, Point3 point) => spawn.SpawnCreatures(creatureType, "T-100", point, 1).Count)
			});
			m_creatureTypes.Add(new CreatureType("T-200", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
			{
				SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
				{
					float num = spawn.m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z);
					int temperature = spawn.m_subsystemTerrain.Terrain.GetTemperature(point.X, point.Z);
					int humidity = spawn.m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z);
					int num2 = Terrain.ExtractContents(spawn.m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z));
					return (num > 60f && Utils.SubsystemGameInfo.WorldSettings.AreSupernaturalCreaturesEnabled && (num2 == 8 || num2 == 2 || num2 == 3 || num2 == 7 || num2 == 12) && Utils.SubsystemTimeOfDay.Day > 100) ? 0.15f : 0f;
				},
				SpawnFunction = ((CreatureType creatureType, Point3 point) => spawn.SpawnCreatures(creatureType, "T-200", point, 1).Count)
			});
			m_creatureTypes.Add(new CreatureType("T-300", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
			{
				SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
				{
					float num = spawn.m_subsystemTerrain.TerrainContentsGenerator.CalculateOceanShoreDistance(point.X, point.Z);
					int temperature = spawn.m_subsystemTerrain.Terrain.GetTemperature(point.X, point.Z);
					int humidity = spawn.m_subsystemTerrain.Terrain.GetHumidity(point.X, point.Z);
					int num2 = Terrain.ExtractContents(spawn.m_subsystemTerrain.Terrain.GetCellValueFast(point.X, point.Y - 1, point.Z));
					return (num < -10f && Utils.SubsystemGameInfo.WorldSettings.AreSupernaturalCreaturesEnabled && Utils.SubsystemTimeOfDay.Day > 140) ? 0.15f : 0f;
				},
				SpawnFunction = ((CreatureType creatureType, Point3 point) => spawn.SpawnCreatures(creatureType, "T-300", point, 1).Count)
			});
		}
    }
}
